<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Google
 *
 * @author Mubashar Ali Bukhari
 */
//require_once '../../googleApiSrc/contrib/apiCalendarService.php';
class GoogleEvent {

    // <editor-fold defaultstate="collapsed" desc="Private Fields Of Google Class">
    //private $access_Token;
    public $event_Id;
    public $calender_Id;
    public $start_Date;
    public $start_DateTime; //Maps With StartDateTime,ActivityDateTime
    public $description;
    public $end_Date;
    public $end_DateTime;
    public $IsAllDayEvent; //Maps With	isset(start.date) ALL Day Event
    public $IsRecurrence; //Maps With	isset(recurrence) Is Recuring Event
    public $IsReminderSet; //Maps With	(reminders.length>0) If Reminder is Set
    public $location; //Location Of Event
    public $reminderDateTime; //Use the last reminder time from Google Calendar as the ReminderDateTime
    public $showAs; //Map With (transparency==opaque?busy:free time)	Busy, Out of Office, Free Time
    public $summary;
    public $Type = "Meeting"; // Should Always Be Meeting
    public $WhoId; //Map with First Attendee...
    public $attendees; //List of Attendees in an Event
    public $updated; //last modified date of Google Event
    public $eventstatus; //event status "cancelled" means event is deleted which should be deleted from Salesforce too.
    public $event_url; //Google event url
    private $client;

    // </editor-fold>
    //<editor-fold defaultstate="collapsed" desc="This Method Will Populate Values From Google Calender, Params can be changed Accordingly.Return Type is GoogleEvent object">
    public function __construct($apiClient = null) {
        $this->client = $apiClient;
    }

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="This Method Will Populate Values From Google Calender, Params can be changed Accordingly.Return Type is GoogleEvent object">
    //
    public function get($calenderId, $eventId, $maxAttendees, $timeZone, array $fields) {
        //Get Values From Google Calender Event And Populate Here..
        //calendar.events.get
        return $this;
    }

    //</editor-fold>
    //<editor-fold>
    //Get Event from Google Calender.
    //</editor-fold>
    public function getevent($calendarId, $eventId, $optfields = array()) {

        //Get Event From Google Calender...
        //calendar.events.get
        $objevent = $events->get($calenderId, $eventId, $optfields);
        $googleEventObject = new GoogleEvent();
        $googleEventObject->start_Date = $objevent->start->date;
        $googleEventObject->start_DateTime = $objevent->start->dateTime;
        $googleEventObject->description = $objevent->description;
        $googleEventObject->end_Date = $objevent->end->date;
        $googleEventObject->end_DateTime = $objevent->end->dateTime;
        //Getting all day event
        if (isset($objevent->start->date)) {
            $googleEventObject->IsAllDayEvent = true;
        } else {
            $googleEventObject->IsAllDayEvent = false;
        }
        //Get recurence
        if (isset($objevent->recurrence)) {
            $googleEventObject->IsRecurrence = true;
        } else {
            $googleEventObject->IsRecurrence = false;
        }
        //Check reminder
        if (count($objevent->reminders) > 0) {
            $googleEventObject->IsReminderSet = true;
        } else {
            $googleEventObject->IsReminderSet = false;
        }
        $googleEventObject->location = $objevent->location;
        //$googleEventObject->reminderDateTime =
        // TODO(Moon): Get ReminderDateTime.
        $googleEventObject->showAs = $objevent->transparency == "transparent" ? "free" : "busy";
        $googleEventObject->calender_Id = $calendarId;
        $googleEventObject->summary = $objevent->summary;
        $googleEventObject->Type = "Meeting";
        $googleEventObject->WhoId = $objevent->attendees[0];
        $googleEventObject->attendees = $objevent->attendees;
        $googleEventObject->event_url = $objevent->htmlLink;
        return $googleEventObject;
    }

    //<editor-fold>
    //Delete Event in Google Calender.
    //</editor-fold>
    public function delete($calendarId, $eventId, $optfields = array()) {

        //Delete Event From Google Calender...
        //calendar.events.delete
        $cal = new apiCalendarService($this->client);
        $events = $cal->events;
        $events->delete($calendarId, $eventId, $optfields);
    }

    //<editor-fold>
    //Insert Event in Google Calender.
    //</editor-fold>
    public function insert($calendarId, GoogleEvent $googleEventObj, $optfields = array()) {

        //Insert Event Into Google Calender...
        //calendar.events.insert

        $cal = new apiCalendarService($this->client);
        $events = $cal->events;
        $objevent = new Event();
        $objevent->start->date = $googleEventObj->start_Date;
        $objevent->start->dateTime = $googleEventObj->start_DateTime;
        $objevent->description = $googleEventObj->description;
        $objevent->end->date = $googleEventObj->end_Date;
        $objevent->end->dateTime = $googleEventObj->end_DateTime;
        //Get recurence
        if ($googleEventObj->IsRecurrence = true) {
            $objevent->recurrence = true;
        }
        //Check reminder
        if ($googleEventObj->IsReminderSet = true) {
            //TODO: add reminder date time.                   
        }
        $objevent->location = $googleEventObj->location;
        if ($googleEventObj->showAs == "busy") {
            $objevent->transparency == "transparent";
        }
        $objevent->summary = $googleEventObj->summary;
        $objevent->attendees = $googleEventObj->attendees;
        //$objevent->htmlLink =  $googleEventObj->event_url;
        $events->insert($calendarId, $objevent, $optfields);
    }

    public static function insertevent($calendarId, GoogleEvent $googleEventObj, $optfields = array()) {

        //Insert Event Into Google Calender...
        //calendar.events.insert

        $cal = new apiCalendarService($this->client);
        $events = $cal->events;
        $objevent = new Event();
        $objevent->start->date = $googleEventObj->start_Date;
        $objevent->start->dateTime = $googleEventObj->start_DateTime;
        $objevent->description = $googleEventObj->description;
        $objevent->end->date = $googleEventObj->end_Date;
        $objevent->end->dateTime = $googleEventObj->end_DateTime;
        //Get recurence
        if ($googleEventObj->IsRecurrence = true) {
            $objevent->recurrence = true;
        }
        //Check reminder
        if ($googleEventObj->IsReminderSet = true) {
            //TODO: add reminder date time.                   
        }
        $objevent->location = $googleEventObj->location;
        if ($googleEventObj->showAs == "busy") {
            $objevent->transparency == "transparent";
        }
        $objevent->summary = $googleEventObj->summary;
        $objevent->attendees = $googleEventObj->attendees;
        //$objevent->htmlLink =  $googleEventObj->event_url;
        

        $events->insert($calendarId, $objevent, $optfields);
    }

    //<editor-fold>
    //Update Event in Google Calender.
    //</editor-fold>
    public function update($calenderId, GoogleEvent $googleEventObj, $optfields = array()) {
        //Get Values From Google Calender Event And Populate Here..
        //calendar.events.update
        $cal = new apiCalendarService($this->client);
        $events = $cal->events;
        $objevent = new Event();
        $objevent->start->date = $googleEventObj->start_Date;
        $objevent->start->dateTime = $googleEventObj->start_DateTime;
        $objevent->description = $googleEventObj->description;
        $objevent->end->date = $googleEventObj->end_Date;
        $objevent->end->dateTime = $googleEventObj->end_DateTime;
        //Get recurence
        if ($googleEventObj->IsRecurrence = true) {
            $objevent->recurrence = true;
        }
        //Check reminder
        if ($googleEventObj->IsReminderSet = true) {
            //TODO: add reminder date time.                   
        }
        $objevent->location = $googleEventObj->location;
        if ($googleEventObj->showAs == "busy") {
            $objevent->transparency == "transparent";
        }
        $objevent->summary = $googleEventObj->summary;
        $objevent->attendees = $googleEventObj->attendees;
        //$objevent->htmlLink =  $googleEventObj->event_url;
        $events->update($calenderId, $googleEventObj->event_Id, $objevent, $optfields);
    }

    public function getEventAttendeeobject() {
        //$cal = new apiCalendarService($this->client);
        $attendee = new EventAttendee();
        return $attendee;
    }

    //<editor-fold>
    //Get Event list of specific calender
    //</editor-fold>
    public function getEventList($calendarId, $optfields = array()) {
        //Get Values From Google Calender Event And Populate Here..
        //calendar.events.list
        //Default Parameter Sets
        //orderBy: startTime
        //showDeleted: true
        //singleEvents: true (Recurring events should be split out into it’s individual corresponding events using the singleEvents=true param.)
        //timeMin: current time – 14 days
        //timeMax: current time + 120 days
//        $calList = $cal->calendarList->listCalendarList();
//        print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
        $cal = new apiCalendarService($this->client);
        $events = $cal->events;
        $eventslist = $events->listEvents($calendarId, $optfields);
        $listeventobj = new Event();
        $listeventobj = $eventslist->items;

//        for ($i = 0; $i <= count($listeventobj); $i++) {
//            $objevent = new Event();   
//            $objevent = $listeventobj[$i];
//            print "evnt objes is <pre>". print_r($objevent) . "</pre>";
//            }
        $googleEventList = array();
        foreach ($listeventobj as $key => $value) {
            $objevent = new Event();
            $objevent = $value;
            $googleEventObject = new GoogleEvent();
            $googleEventObject->event_Id = $objevent->id;
            $googleEventObject->start_Date = $objevent->start->date;
            $googleEventObject->start_DateTime = $objevent->start->dateTime;
            $googleEventObject->description = $objevent->description;
            $googleEventObject->end_Date = $objevent->end->date;
            $googleEventObject->end_DateTime = $objevent->end->dateTime;
            //Getting all day event
            if (isset($objevent->start->date)) {
                $googleEventObject->IsAllDayEvent = true;
            } else {
                $googleEventObject->IsAllDayEvent = false;
            }
            //Get recurence
            if (isset($objevent->recurrence)) {
                $googleEventObject->IsRecurrence = true;
            } else {
                $googleEventObject->IsRecurrence = false;
            }
            //Check reminder
            if (count($objevent->reminders) > 0) {
                $googleEventObject->IsReminderSet = true;
            } else {
                $googleEventObject->IsReminderSet = false;
            }
            $googleEventObject->location = $objevent->location;
            //$googleEventObject->reminderDateTime =
            // TODO(Moon): Get ReminderDateTime.
            $googleEventObject->showAs = $objevent->transparency == "transparent" ? "free" : "busy";
            $googleEventObject->calender_Id = $calendarId;
            $googleEventObject->summary = $objevent->summary;
            $googleEventObject->Type = "Meeting";
            $googleEventObject->WhoId = $objevent->attendees[0];
            $googleEventObject->attendees = $objevent->attendees;
            $googleEventObject->updated = $objevent->updated;
            $googleEventObject->event_url = $objevent->htmlLink;
            //array_push($googleEventList, $googleEventObject);
            $googleEventList[$googleEventObject->event_Id] = $googleEventObject;
        }
        return $googleEventList;
    }

    public function getCalendarList() {
        //Get Values From Google Calender Event And Populate Here..
        //calendar.events.list
        //Default Parameter Sets
        //orderBy: startTime
        //showDeleted: true
        //singleEvents: true (Recurring events should be split out into it’s individual corresponding events using the singleEvents=true param.)
        //timeMin: current time – 14 days
        //timeMax: current time + 120 days
//        $calList = $cal->calendarList->listCalendarList();
//        print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
        $cal = new apiCalendarService($this->client);
        $calList = $cal->calendarList->listCalendarList();
        CalendarSyncUtil::debug('calList', $calList);
    }

}

?>
